﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Globalization;
using HA.Data;
using HA.ModFlow.Domain.DomainLayer;




namespace HA.ModFlow.Controls.Windows.InternalFlow
{
    /// <summary>
    /// winLPFFlow.xaml 的交互逻辑
    /// </summary>
    public partial class winLPFFlow : Window
    {
        public winLPFFlow(Project proj)
        {
            InitializeComponent();
            project = proj;
            ModFlowData mfData = proj.MfData;
            this._lpfFlow = new PCLPFFlow(mfData.FlowData.LpfData);

            this._mdOption = mfData.ModelOption;

            //层
            ComboBoxItem cbi;
            for (int i = 0; i < this._lpfFlow.ClonedCopy.Layers.Count; i++)
            {
                cbi = new ComboBoxItem();
                cbi.Content = (i + 1).ToString();
                cboLayer.Items.Add(cbi);
            }

            if (cboLayer.Items.Count > 0)
            {
                _currentLayer = _lpfFlow.ClonedCopy.Layers[0];
                cboLayer.SelectedIndex = 0;
            }

          
            this.DataContext = this._lpfFlow;
            this.grdLayer.DataContext = this._currentLayer;
       
            Binding binding = new Binding();
            binding.Source = _currentLayer;
            binding.Path = new PropertyPath("LayerType");
            binding.Converter = new LayerTypeToIntConverter();
            this.comboBox1.SetBinding(ComboBox.SelectedIndexProperty, binding);
            //this._lpfFlow.LpfData.Parent.IsHaveTransient;

            UpdatabtnSsAndbtnSy();

            this.btnSs.IsEnabled = this._lpfFlow.LpfData.Parent.IsHaveTransient;
            this.btnSy.IsEnabled = this._lpfFlow.LpfData.Parent.IsHaveTransient;
            IsShowWater();
        }
        Project project;

        ModelOption _mdOption;

        /// <summary>
        /// 标记隔水层垂向导水系数按钮是否进行灰化
        /// </summary>
        public bool isVkcb = false;
        /// <summary>
        /// 比容量状态和单位水产量
        /// </summary>
        private void UpdatabtnSsAndbtnSy()
        {
            
            if (rbtnLAYVKA_1.IsChecked.Value)
            {
                this.btnVKA.Content = "垂向渗透系数";
            }
            else
            {
                this.btnVKA.Content = "垂向渗透系数比率";
            }
        }

        private void IsShowWater()
        {
            if (this._lpfFlow.ClonedCopy.IsStorageCoefficient)
            {
                this.btnSs.Content = "贮水系数";
            }
            else
            {
                this.btnSs.Content = "贮水率";
            }
        }

        PCLPFFlow _lpfFlow;

        LPFLayer _currentLayer;
        

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._lpfFlow.Confirm();
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {            
            this.Close();
        }

        private void cboLayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboLayer.SelectedIndex >= 0)
            {
                _currentLayer = this._lpfFlow.ClonedCopy.Layers[cboLayer.SelectedIndex];           
                Binding binding = new Binding();
                binding.Source = _currentLayer;
                binding.Path = new PropertyPath("LayerType");
                binding.Converter = new LayerTypeToIntConverter();
                this.comboBox1.SetBinding(ComboBox.SelectedIndexProperty, binding);
                this.grdLayer.DataContext = _currentLayer;
                if (!_currentLayer.LAYVKAIsValue)
                {
                    rbtnLAYVKA_1.IsChecked = true;
                }
                chkAllow_Click(sender, e);
                UpdatabtnSsAndbtnSy();
            }
        }      

        private void btnHK_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Parent.Hk,project);
            win.Owner = this;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(_mdOption.Units.Time) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnVKA_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win;
            if (this.rbtnLAYVKA_1.IsChecked.Value)
            {
                win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Vka, project,true );
            }
            else
            {
                win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Vka, project,false);
            }
            win.Owner = this;
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(_mdOption.Units.Time) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnHorAni_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Hani, project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnSs_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Ss, project);
            win.Owner = this;
            win.Unit += "(1/" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + ")";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnSy_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Sy, project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnWetDryFlag_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Parent.WetDry, project);
            win.Owner = this;
            win.Unit += "无量纲";
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

        private void btnVKCB_Click(object sender, RoutedEventArgs e)
        {
            winCellCenterDSEdit win = new winCellCenterDSEdit(this._lpfFlow.ClonedCopy.Vkcb, project);
            win.Unit += "(" + Domain.DomainLayer.UnitConverter.GetLength(_mdOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(_mdOption.Units.Time) + ")";
            win.Owner = this;
            win.ShowDialog();
            if (project.CurrentDSData != null && project.CurrentDSData.Label == win.PcDs.Dataset.Label)
                project.OnCurrentDSChanged();
        }

      

        private void chkAllow_Click(object sender, RoutedEventArgs e)
        {
            if (this.chkAllow.IsChecked.Value)
            {
                this.btnWetDryFlag.IsEnabled = true;
            }
            else
            {
                this.btnWetDryFlag.IsEnabled = false;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            chkAllow_Click(sender, e);
            if (this._lpfFlow.ClonedCopy.Layers.Count > 0)
            {
                this.cboLAYAVG.SelectedIndex = this._lpfFlow.ClonedCopy.Layers[0].LAYAVG;
            }
            this.btnVKCB.IsEnabled = isVkcb;
        }

        private void btnMoreOption_Click(object sender, RoutedEventArgs e)
        {
            winLPFMoreOption win = new winLPFMoreOption(this ._lpfFlow.ClonedCopy);
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog().Value)
            {
                this._lpfFlow.ClonedCopy = win.PcLPF;
                IsShowWater();
            }
        }

        private void rbtnLAYVKA_0_Click(object sender, RoutedEventArgs e)
        {
            UpdatabtnSsAndbtnSy();
        }

        private void rbtnLAYVKA_1_Click(object sender, RoutedEventArgs e)
        {
            UpdatabtnSsAndbtnSy();
        }

      

    }
    public class LayerTypeToIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((LPFLAYERTYPE)value == LPFLAYERTYPE.CONFINED)
            {
                return 0;
            }
            else
            {
                if ((LPFLAYERTYPE)value == LPFLAYERTYPE.CONVERTIBLE)
                {
                    return 1;
                }
                else
                    return 2;
            }

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch ((int)value)
            {
                case 0:
                    return LPFLAYERTYPE.CONFINED;

                case 1:
                    return LPFLAYERTYPE.CONVERTIBLE;

                case 2:
                    return LPFLAYERTYPE.CONVERTIBLEONTHICKSTRT;

                default:
                    return LPFLAYERTYPE.CONVERTIBLEONTHICKSTRT;
            }

        }
    }
}
